package com.cskaoyan.mall.controller.admin;

import com.cskaoyan.mall.bean.Category;
import com.cskaoyan.mall.bean.vo.CategoryL1VO;
import com.cskaoyan.mall.bean.vo.CategoryLevelOne;
import com.cskaoyan.mall.bean.vo.CategoryVO;
import com.cskaoyan.mall.bean.vo.ResultVO;
import com.cskaoyan.mall.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: dyj
 * @Date: 2021/1/10 19:05
 * @Description 商场管理 商品类目模块
 */
@RestController
@RequestMapping("/admin/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    @RequestMapping("l1")
    public ResultVO level1(){
        List<CategoryL1VO> categoryL1VOList = categoryService.selectLevelOne();
        return ResultVO.ok(categoryL1VOList);
    }


    @RequestMapping("list")
    public ResultVO list(){
        List<CategoryLevelOne> categoryList = categoryService.selectAll();
        return ResultVO.ok(categoryList);
    }

    @RequestMapping("create")
    public ResultVO create(@RequestBody Category category){
        CategoryVO categoryVo = categoryService.insertSelective(category);
        return ResultVO.ok(categoryVo);
    }

    @RequestMapping("update")
    public ResultVO update(@RequestBody Category category){
        // TODO bug level 1 修改为 level 2 未添加父目录 会消失 （子目录也存在该问题）
        categoryService.updateCategory(category);
        return ResultVO.ok();
    }

    @RequestMapping("delete")
    public ResultVO delete(@RequestBody CategoryLevelOne categoryLevelOne){
        categoryService.deleteCategory(categoryLevelOne);
        return ResultVO.ok();
    }
}
